package com.yungui.kafka.consumer.util;

import org.apache.commons.collections.MapUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 */
public class HttpClientUtil {
    public static void main(String[] args) throws IOException {
        String content = HttpClientUtil.doGet("http://www.baidu.com");
        System.out.println(content);
    }

    /**
     * 发送 post请求 body传json数据
     * @throws IOException
     */
    public static String doPost(String url, String json) throws IOException {
        return doPost(url, json, null);
    }

    /**
     * 发送post请求
     * body传json数据
     *
     * @param url     url请求
     * @param json    请求参数
     * @param headers 请求header
     * @return 请求结果
     * @throws IOException 请求异常
     */
    public static String doPost(String url, String json, Map<String, String> headers) throws IOException {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            if (MapUtils.isNotEmpty(headers)) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    httpPost.setHeader(header.getKey(), header.getValue());
                }
            }
            String response = "";
            StringEntity requestEntity = new StringEntity(json, "UTF-8");
            httpPost.setEntity(requestEntity);
            try (CloseableHttpResponse httpResponse = httpclient.execute(httpPost);) {
                if (httpResponse == null) {
                    response = null;
                }
                HttpEntity responseEntity = httpResponse.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                response = EntityUtils.toString(responseEntity);
                return response;
            }
        }
    }
    /**
     * 发送 post请求 传键值对
     * @throws IOException
     */
    public static String doPost(String url, Map<String, String> params) throws IOException {
        try (CloseableHttpClient httpclient = HttpClients.createDefault();) {
            HttpPost httpPost = new HttpPost(url);
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            for (String name : params.keySet()) {
                nvps.add(new BasicNameValuePair(name, params.get(name)));
            }
            String response = "";
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            try (CloseableHttpResponse httpResponse = httpclient.execute(httpPost);) {
                if (httpResponse == null) {
                    response = null;
                }
                HttpEntity entity2 = httpResponse.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                response = EntityUtils.toString(entity2);
                return response;
            }
        }
    }

    /**
     * 发送 get请求
     * @throws IOException
     */
    public static String doGet(String url) throws IOException {
        String content = null;
        try (CloseableHttpClient httpclient = HttpClients.createDefault();) {
            HttpGet httpget = new HttpGet(url);
            try (CloseableHttpResponse response = httpclient.execute(httpget);) {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    content = EntityUtils.toString(entity);
                }
            }
        }
        return content;
    }

    /**
     * 发送请求头的get请求
     *
     * @param url
     * @param headers
     * @return
     */
    public static String doGet(String url, Map<String, String> headers) throws IOException {
        String content = null;
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            if (httpGet != null && MapUtils.isNotEmpty(headers)) {
                for (Map.Entry<String, String> e : headers.entrySet()) {
                    httpGet.addHeader(e.getKey(), e.getValue());
                }
            }
            try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    content = EntityUtils.toString(entity);
                }
            }
        }
        return content;
    }

}
